package com.wannengbang.cloudleader.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.wannengbang.cloudleader.R;
import com.wannengbang.cloudleader.bean.ShopCartListBean;
import com.wannengbang.cloudleader.bean.ShopListBean;
import com.wannengbang.cloudleader.utils.GlideUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 消息列表
 */
public class ShopCartListAdapter extends RecyclerView.Adapter<ShopCartListAdapter.ViewHolder> {

    private List<ShopListBean.DataBean.ItemListBean> listsBeans;
    private Context context;
    private onCallBackListener onCallBackListener;

    public ShopCartListAdapter(List<ShopListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
    }

    public void setOnCallBackListener(ShopCartListAdapter.onCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_shop_cart_goods_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ShopListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        dataBean.setName(dataBean.getGoods_name());
        GlideUtils.loadImage(context, holder.ivGoodsThumb, dataBean.getThumb());
        holder.tvGoodsName.setText(dataBean.getGoods_name());
        holder.tvNumber.setText(dataBean.getGoods_amount() + "");
        holder.tvGoodsPrice.setText(dataBean.getIntegral_price());
        holder.tvGoodsNum.setText("x" + dataBean.getGoods_amount());

        if (dataBean.isCheck()) {
            holder.ivItemChoose.setImageResource(R.mipmap.ic_order_xuanze);
        } else {
            holder.ivItemChoose.setImageResource(R.mipmap.ic_order_kongyuan);
        }

        holder.ivItemChoose.setOnClickListener(v -> {
            onCallBackListener.onCallBack(3, position);
        });
        holder.tvSort.setOnClickListener(v -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvAdd.setOnClickListener(v -> {
            onCallBackListener.onCallBack(2, position);
        });
    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_item_choose)
        ImageView ivItemChoose;
        @BindView(R.id.iv_goods_thumb)
        RoundedImageView ivGoodsThumb;
        @BindView(R.id.tv_goods_name)
        TextView tvGoodsName;
        @BindView(R.id.tv_goods_price)
        TextView tvGoodsPrice;
        @BindView(R.id.tv_goods_num)
        TextView tvGoodsNum;
        @BindView(R.id.tv_sort)
        TextView tvSort;
        @BindView(R.id.tv_number)
        TextView tvNumber;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.ll_root)
        LinearLayout llRoot;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface onCallBackListener {
        void onCallBack(int type, int position);
    }
}
